//
//  playViewController.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "playViewController.h"
#import "SingerState.h"
#import "SingerModel.h"
#import "PlaySongListCell.h"
#import "myButton.h"
#import <AFNetworking/AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "DataBaseHandler.h"
#import "CollectDatabase.h"
static playViewController *play = nil;

@interface playViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>



@end

@implementation playViewController

+ (playViewController *)shareStateOfViewcontroller
{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        if (!play) {
            play = [[playViewController alloc]init];
        }
    });
    return play;
}
- (void)loadView
{
    [super loadView];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.hidesBottomBarWhenPushed = YES;
    self.playScroll = [[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)] autorelease];
    
    self.playScroll.pagingEnabled = YES;
    //    [self.playScroll setBackgroundColor:[UIColor yellowColor]];
    self.playScroll.userInteractionEnabled = YES;
    self.playScroll.delegate = self;
    self.playScroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 3, 0);
    [self.view addSubview:self.playScroll];
    //    self.myImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    self.myImageview = [[UIImageView alloc]init];
    [self.myImageview setBackgroundColor:[UIColor whiteColor]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.view.backgroundColor =[UIColor clearColor];
    self.playState = PlayWithWayCirculation;
    [self builtInterface];
    self.mySlider = [[[UISlider alloc]initWithFrame:CGRectMake(20, -0, [UIScreen mainScreen].bounds.size.width - 40, 0)] autorelease];
    [self.mySlider addTarget:self action:@selector(sliderAction) forControlEvents:UIControlEventValueChanged];
    //  self.navigationController.navigationBar.translucent = YES;
    
    UIEvent *event = [[UIEvent alloc]init];
//    [self.mySlider beginTrackingWithTouch:0 withEvent:event];
    [self.playview addSubview:self.mySlider];
    //    [self getarray];
    [self builtSongList];
    
    
    
    
    
    
}

- (void)builtSongList
{
    self.songListTableview = [[[UITableView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 2 , 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 * 2 - 20) style:UITableViewStylePlain] autorelease];
    [self.songListTableview setBackgroundColor:[UIColor greenColor]];
    self.songListTableview.delegate = self;
    self.songListTableview.dataSource = self;
    [self.songListTableview setBackgroundColor:[UIColor clearColor]];
    [self.songListTableview registerClass:[PlaySongListCell class] forCellReuseIdentifier:@"cell"];
    //    [self.view addSubview:self.songListTableview];
    
    UIView *view = [[[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 2, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)] autorelease];
    [view setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4 ]];
    [self.playScroll addSubview:view];
    
    [self.playScroll addSubview:self.songListTableview];
}
- (void)builtInterface
{
   
    int a = 55;
    
    self.playview = [[[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 70 , [UIScreen mainScreen].bounds.size.width, 70)] autorelease];
    [self.playview setBackgroundColor:[UIColor colorWithRed:0.05 green:0.1 blue:0.2 alpha:.2]];
    float b = ([UIScreen mainScreen].bounds.size.width - a) / 2;
    self.playButton = [[[UIButton alloc]initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - a) / 2 + 5, 10, a- 5, a - 5)] autorelease];
    //    [self.playButton setBackgroundColor:[UIColor whiteColor]];
    UIImage *Image = [UIImage imageNamed:@"stop.png"];
    Image = [Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.playButton setBackgroundImage:Image forState:UIControlStateNormal];
    
    [self.playview addSubview:self.playButton];
    
    [self.playButton addTarget:self action:@selector(musicPlay) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.lastbutton = [[[UIButton alloc]initWithFrame:CGRectMake(b / 2 + 10, 15, a - 20, a - 20)] autorelease];
    //    [self.lastButton setBackgroundColor:[UIColor greenColor]];
    [self.lastbutton addTarget:self action:@selector(lastButton)forControlEvents:UIControlEventTouchUpInside];
    UIImage *lastimage = [UIImage imageNamed:@"last.png"];
    lastimage = [lastimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.lastbutton setBackgroundImage:lastimage forState:UIControlStateNormal];
    [self.playview addSubview:self.lastbutton];
    self.nextBButton = [[[UIButton alloc]initWithFrame:CGRectMake((b /2 - a + 10) + b + a, 15, a - 20, a - 20)] autorelease];
    [self.nextBButton addTarget:self action:@selector(nextButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIImage *nextimage = [UIImage imageNamed:@"next.png"];
    nextimage  = [nextimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.nextBButton setBackgroundImage:nextimage forState:UIControlStateNormal];
    [self.playview addSubview:self.nextBButton];
    
    self.xunhuanButton = [[[UIButton alloc]initWithFrame:CGRectMake(10+15, 15, a - 20, a - 20)] autorelease];
    //    [self.xunhuanButton setBackgroundColor:[UIColor redColor]];
    [self.xunhuanButton addTarget:self action:@selector(playStateButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIImage *xunimage = [UIImage imageNamed:@"xun.png"];
    xunimage = [xunimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.xunhuanButton setBackgroundImage:xunimage forState:UIControlStateNormal];
    [self.playview addSubview:self.xunhuanButton];
    
    self.xiangbutton = [[[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 10 - a + 10, 15, a - 10, a - 10)] autorelease];
    UIImage *image = [UIImage imageNamed:@"dontlike"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImageView *xiangimageview = [[[UIImageView alloc]initWithFrame:CGRectMake(4, 5, 30, 30)] autorelease];
    xiangimageview.image = image;
    [self.xiangbutton addSubview:xiangimageview];
    [self.xiangbutton addTarget:self action:@selector(songListAction) forControlEvents:UIControlEventTouchUpInside];
    [self.playview addSubview:self.xiangbutton];
    
    [self.view addSubview:self.playview];
    //    [self.playScroll addSubview:self.playview];
    
    //    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(returnbuttonaction)];
    
    
    
    [self builtlabel];
    
    
    
    self.myTime = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeaction) userInfo:nil repeats:YES];
    
}
- (void)builtlabel{
    
    SingerState * singer = [SingerState shareOfPlayState];
    SingerModel *model = [singer.playListAddress objectAtIndex:singer.num];
    
//    NSString *str = model.singerName;
//    NSString *str2 = model.name;
//    
//    NSDictionary *nameDic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
//    CGRect nameRect = [str2 boundingRectWithSize:CGSizeMake(0, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameDic context:nil];
//    
//    
//    //歌手
//    NSDictionary *singerdic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
//    CGRect singerRect = [str boundingRectWithSize:CGSizeMake(0, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:singerdic context:nil];
//    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, nameRect.size.width, 20)];
//    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, singerRect.size.width, 15)];
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 100, 35)];
//    //    [label setBackgroundColor:[UIColor redColor]]; [label2 setBackgroundColor:[UIColor yellowColor]];
//    label.text = str2; // 歌曲名
//    label2.text = str;  //歌手名
//    label2.font = [UIFont systemFontOfSize:15];
//    [view addSubview:label];
//    [view addSubview:label2];
//    [view setBackgroundColor:[UIColor greenColor]];
//    
//    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(returnbuttonaction)] autorelease];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    [button setBackgroundColor:[UIColor clearColor]];
    button.frame = CGRectMake(20, 20, 100, 35);
//    [button setTitle:@"return" forState:UIControlStateNormal];
    UIImage *image = [UIImage imageNamed:@"return.png"];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(returnbuttonaction) forControlEvents:UIControlEventTouchUpInside];
//    UIImage *imagess = [UIImage imageNamed:@"last.png"];
//    imagess = [imagess imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    [button setBackgroundImage:imagess forState:UIControlStateNormal];
    [self.view addSubview:button];
//    [view addGestureRecognizer:tap];
    //    self.navigationItem.leftBarButtonItem = [UIBarButtonItem alloc]initWithCustomView:<#(UIView *)#>
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
//    [self.view addSubview:view];
}

- (void)stopaction
{
    [self.audio stop];
    SingerState *singer = [SingerState shareOfPlayState];
    singer.name = @"停止";
    
    NSLog(@"停止播放");
}
- (void)musicPlay
{
    SingerState *single = [SingerState shareOfPlayState];
    NSString *str = [NSString stringWithFormat:@"播放"];
    if ([str isEqualToString:single.name]) {
        [self.audio pause];
        NSLog(@"暂停");
        UIImage *image = [UIImage imageNamed:@"iconfont-bofang.png"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.playButton setBackgroundImage:image forState:UIControlStateNormal];
        single.name = @"暂停";
    } else if ([single.name isEqualToString:@"停止"])
    {
        [self.audio stop];
        self.audio = [[AudioStreamer alloc]initWithURL:single.PlayUrl];
        single.playTime = self.audio.duration;
        single.name = @"播放";
        [self.audio start];
        [self requestLyricNet];
        
    }
    else if ([single.name isEqualToString:@"暂停"]){
        [self.audio start];
        UIImage *image = [UIImage imageNamed:@"stop.png"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.playButton setBackgroundImage:image forState:UIControlStateNormal];
        single.name = @"播放";
    } else
    {
        single.name = @"播放";
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", single.PlayUrl]];
        self.audio = [[AudioStreamer alloc]initWithURL:url];
        single.playTime = self.audio.duration;
        [self.audio start];
        [self requestLyricNet];
        single.PlayUrl = url; //设置正在播放的单例
        
        
    }
}
- (void)play
{
    SingerState *single = [SingerState shareOfPlayState];
    
    [self.audio stop];
    self.audio = [[AudioStreamer alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.songaddress]]];
    [self requestLyricNet];
    single.playTime = self.audio.duration;
    [self.audio start];
    
    SingerState *singer= [SingerState shareOfPlayState];
    singer.name = @"播放";
    
}
- (void)nextButton
{
    [self.audio stop];
    SingerState *singer = [SingerState shareOfPlayState];
    singer.num ++;
    if (singer.xunhuanfangshi == PlayWithWayCirculation || singer.xunhuanfangshi == PlayWithWaySole) {
        
        singer.name = @"停止";
        if (singer.num == [singer.playListAddress count]) {
            singer.num = 0;
        }
        SingerModel *model = [singer.playListAddress objectAtIndex:singer.num];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", model.url]];
        
        self.audio = [[AudioStreamer alloc]initWithURL:url];
        [self requestLyricNet];
        singer.playTime = self.audio.duration;
        [self.audio start];
        
        singer.PlayUrl = url;//设置正在播放的单例
        if (singer.addNum != 0) {
            singer.addNum --;
        }
        
    } else if (singer.xunhuanfangshi == PlayWithWayRandom)//随机播放
    {
        if (singer.addNum == 0) {
            NSInteger i = [singer.playListAddress count];
            NSInteger arc = arc4random() %(i - 0 + 1) + 0;
            if (arc > i) {
                arc -= (i / 2);
            }
            SingerModel *model = [singer.playListAddress objectAtIndex:arc];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", model.url]];
            
            singer.num = arc;
            self.audio = [[AudioStreamer alloc]initWithURL:url];
            singer.playTime = self.audio.duration;
            [self.audio start];
            [self requestLyricNet];
            singer.PlayUrl = url;//设置正在播放的单例
        } else {  //如果添加过的话
            SingerModel *model = [singer.playListAddress objectAtIndex:singer.num];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", model.url]];
            self.audio = [[[AudioStreamer alloc]initWithURL:url] autorelease];
            [self requestLyricNet];
            singer.playTime = self.audio.duration;
            [self.audio start];
            singer.addNum--;
            
        }
    }
    NSLog(@"下一曲");
    singer.name = @"播放";
    
}



- (void)lastButton
{
    SingerState *singer = [SingerState shareOfPlayState];
    if (singer.xunhuanfangshi == PlayWithWayCirculation || singer.xunhuanfangshi == PlayWithWaySole) {
        
        
        singer.num --;
        [self.audio stop];
        singer.name = @"停止";
        if (singer.num < 0) {
            singer.num = [singer.playListAddress count] - 1;
        }
        
        SingerModel *model = [singer.playListAddress objectAtIndex:singer.num];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", model.url]];
        
        self.audio = [[AudioStreamer alloc]initWithURL:url];
        singer.playTime = self.audio.duration;
        [self.audio start];
        [self requestLyricNet];
        singer.PlayUrl = url; //设置正在播放的单例
        
        if (singer.addNum != 0) {
            singer.addNum --;
        }
        
    } else if (singer.xunhuanfangshi == PlayWithWayRandom)//随机
    {
        
        [self.audio stop];
        NSInteger i = [singer.playListAddress count];
        NSInteger arc = arc4random() %(i - 0 + 1) + 0;
        
        SingerModel *model = [singer.playListAddress objectAtIndex:arc - 1];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", model.url]];
        
        singer.num = arc;
        self.audio = [[AudioStreamer alloc]initWithURL:url];
        [self requestLyricNet];
        singer.playTime = self.audio.duration;
        [self.audio start];
        singer.PlayUrl = url; //设置正在播放的单例
    }
    singer.name = @"播放";
    
}
- (void)returnbuttonaction
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    [self.delegate sendState:YES]; //yes 就弹出
    
}

- (void)buttonAction
{
    
    
}
//判断循环方式
- (void)playStateButtonAction
{
    
    SingerState *singer = [SingerState shareOfPlayState];
    if (singer.xunhuanfangshi == PlayWithWayCirculation) {
        singer.xunhuanfangshi = PlayWithWayRandom;
        UIImage *image = [UIImage imageNamed:@"random.png"];
        image  = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.xunhuanButton setBackgroundImage:image forState:UIControlStateNormal];
        
        NSLog(@"随机");
    } else if (singer.xunhuanfangshi == PlayWithWayRandom)
    {
        singer.xunhuanfangshi = PlayWithWaySole;
        UIImage *image = [UIImage imageNamed:@"xunhuan.png"];
        image  = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.xunhuanButton setBackgroundImage:image forState:UIControlStateNormal];
        NSLog(@"单曲循环");
    } else if (singer.xunhuanfangshi == PlayWithWaySole)
    {
        singer.xunhuanfangshi = PlayWithWayCirculation;
        
        UIImage *image = [UIImage imageNamed:@"xun.png"];
        image  = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.xunhuanButton setBackgroundImage:image forState:UIControlStateNormal];
        NSLog(@"循环");
    }
}
// 时间控制
- (void)timeaction
{
    
    
    SingerState *singer = [SingerState shareOfPlayState];
    
    if (singer.playTime <= 3 && self.audio.progress > 2) {
        SingerModel *model = [singer.playListAddress objectAtIndex:singer.num];
        NSInteger flo = [model.duration integerValue];
        singer.playTime = flo / 1000;
        
    } else if (singer.playTime >= 3 && self.audio.progress + 4 >= singer.playTime) {
        [self.audio stop];
        if (!(singer.xunhuanfangshi == PlayViewWithWaySole)) {
            [self nextButton];
        } else
        {
            self.audio = [[AudioStreamer alloc]initWithURL:singer.PlayUrl];
            [self requestLyricNet];
            singer.playTime = self.audio.duration ;
            [self.audio start];
        }
    }
    //        NSLog(@"%ld - %f", singer.playTime, self.audio.progress);
    // 控制slider
    if (self.audio.progress < 2) {
        self.mySlider.value = 0;
    } else {
        self.mySlider.value = self.audio.progress / singer.playTime;
    }
    if (singer.numOfTime  < [singer.lyricArray count] - 5) {
        NSDictionary *timedic = [singer.lyricArray objectAtIndex:singer.numOfTime];
        NSString *str = [timedic objectForKey:@"time"];
        NSInteger inter = [str integerValue]; //  歌词时间
        NSInteger inte = self.audio.progress;  //  播放时间
        
        if (inter < inte) {
            [self.lyricListTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:singer.numOfTime inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
            singer.numOfTime++;
        }
        //        NSLog(@"%ld", singer.numOfTime);
    }
    //    for (NSInteger i = singer.numOfTime; i < [singer.lyricArray count]; i++) {
    //
    //    }
    
    
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.playScroll) {
        NSLog(@"%f", scrollView.contentOffset.x);
        CGFloat floa = [UIScreen mainScreen].bounds.size.width / 45;
        
        CGFloat aa = ([UIScreen mainScreen].bounds.size.height - [UIScreen mainScreen].bounds.size.width) / 3 + floa;
        
        self.picutureScroll.contentOffset = CGPointMake((scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width - 1) * aa, 0);
    }
    if (scrollView == self.lyricListTableView) {
        NSLog(@"%f", scrollView.contentOffset.y);
    }
    
    
//    
    
}


//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
////    NSLog(@"%f", scrollView.contentSize.height);
////    NSLog(@"%f, %f,",scrollView.contentOffset.x, scrollView.contentOffset.y);
//}
// 拖动进度条
- (void)sliderAction
{
    [self.myTime invalidate];
    
    SingerState *single = [SingerState shareOfPlayState];
    //    self.mySlider.value = self.audio.progress / single.playTime;
    [self.audio seekToTime:self.mySlider.value * single.playTime];
    
    self.myTime = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeaction) userInfo:nil repeats:YES];
    
}

- (void)autoStartPlay
{
    SingerState *single = [SingerState shareOfPlayState];
    NSString *str = [NSString stringWithFormat:@"%@", self.songaddress];
    
    if ([single.PlayUrl isEqual:[NSURL URLWithString:str]]) {
        
        
    }
}
- (void)playWithModel:(NSMutableArray *)modelArray index:(NSInteger)index
{
    [self.delegate sendState:NO];
    SingerModel *model = [modelArray objectAtIndex:index];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", model.url]];
    SingerState *single = [SingerState shareOfPlayState];
    single.num = index;  //播放顺序
    if ([single.PlayUrl isEqual:url]) { // && [single.playListAddress isEqualToArray:array]
        // 播放继续
        single.name = @"播放";
        [self judgeMusic];
    } else
    {
        [self.audio stop];
        single.PlayUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", model.url]];
#warning 赋给播放model内容
        single.playListAddress = [NSMutableArray arrayWithArray:modelArray];  //在这儿赋给播放model内容
        single.addNum = 0;
        single.name = @"停止";
        [self judgeMusic];
    }
    [self.songListTableview reloadData];
}
- (void)judgeMusic
{
    SingerState *singer = [SingerState shareOfPlayState];
    if ([singer.name isEqualToString:@"停止"]) { //要切歌
        [self.audio stop];
        self.audio = [[AudioStreamer alloc]initWithURL:singer.PlayUrl];
        [self requestLyricNet];
        singer.playTime = self.audio.duration;
        [self.audio start];
        singer.name = @"播放";
    }
}
- (void)songListAction
{
    SingerState *singer = [SingerState shareOfPlayState];
    SingerModel *model = [singer.playListAddress objectAtIndex:singer.num];
    [CollectDatabase insertWithModel:model];
    
    UIImage *image = [UIImage imageNamed:@"like"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImageView *imageview = [[[UIImageView alloc]initWithFrame:CGRectMake(4, 5, 30, 30)] autorelease];
    imageview.image = image;
    [self.xiangbutton addSubview:imageview];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SingerState *single =[SingerState shareOfPlayState];
    if (tableView == self.songListTableview) {
        return [single.playListAddress count];
    }
    if (tableView == self.lyricListTableView) {
        return [self.lyricArray count] + 5;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.songListTableview) {
        
        PlaySongListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        SingerState *single = [SingerState shareOfPlayState];
        SingerModel *model = [single.playListAddress objectAtIndex:indexPath.row];
        NSString *str = [NSString stringWithFormat:@"%@-%@", model.name, model.singerName];
        NSDictionary *dic17 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [str boundingRectWithSize:CGSizeMake(0, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic17 context:nil];
        cell.singleSongLabel.frame = CGRectMake(34, 2, rect.size.width, 30);
        cell.singleSongLabel.text = [NSString stringWithFormat:@"%@-%@", model.name, model.singerName];
        [cell.button addTarget:self action:@selector(addButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //    [cell.button addButtonAction:self];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.singleSongLabel.textColor = [UIColor whiteColor];
        cell.button.row = indexPath.row;
        
        
//        [cell setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.5]];
        return cell;
    }
    if (tableView == self.lyricListTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellcell"];
        NSDictionary *dic = [self.lyricArray objectAtIndex:indexPath.row];
        //        cell.textLabel.font = [UIFont systemFontOfSize:];
        cell.textLabel.text = [NSString stringWithFormat:@"%@", [dic objectForKey:@"lyric"]];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    return nil;
}


- (void)addButtonAction:(myButton *)sender
{
    myButton *button = (myButton *)sender;
    SingerState *singer = [SingerState shareOfPlayState];
    SingerModel *model = [singer.playListAddress objectAtIndex:button.row];
    singer.addNum ++;  //添加数量
    
    [singer.playListAddress insertObject:model atIndex:singer.num + singer.addNum];
    playViewController *play = [[[playViewController alloc]init] autorelease];
    [play.songListTableview reloadData];
    
    
    NSLog(@"插入%@", model.name);
    
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
////    return 34;
//}
#warning 这儿得修改随机播放的bug
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.songListTableview) {
        [self.audio stop];
        SingerState *singer = [SingerState shareOfPlayState];
        singer.num = indexPath.row;
        SingerModel *model = [singer.playListAddress objectAtIndex:singer.num];
        self.audio = [[AudioStreamer alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", model.url]]];
        [self requestLyricNet];
        singer.playTime = self.audio.duration ;
        [self.audio start];
        singer.addNum = 0;
    }
}
//////////////////////8888888888888888切歌还有bug!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 已解决

//请求歌词和图片
- (void)requestLyricNet
{
    SingerState *singer = [SingerState shareOfPlayState];
    SingerModel *model =[singer.playListAddress objectAtIndex:singer.num];
    
    NSArray * array = [model.name componentsSeparatedByString:@"("];
    NSString * str = [array objectAtIndex:0];
    //    NSLog(@"%@", str);  // 伟大的拼接
    NSString *address = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString * add1 = @"http://api.raventech.cn/api/music/search?album_name=&appkey=54679361&appversion=ios1.3.0&cate_type=0&client_time=2015-05-21%2015%3A28%3A39&content=";
    NSString *add2 =  @"&deviceid=86D47CC5-7337-4186-98C3-6EB77C5C8CD3&deviceusername=%25E5%25A4%25AA%25E9%2598%25B3&key=";
    NSString *str3 =  @"&network_type=1&page=1&searchtype=0&singer_name=&song_name=&type=0&zone=Asia%252FShanghai%2520%2528GMT%252B8%2529";
    NSString *superAddress = [NSString stringWithFormat:@"%@%@%@%@%@", add1,address,add2,address,str3];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:superAddress parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSData *data = [NSData dataWithData:responseObject];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //        NSLog(@"*********************************************/n%@/n***************************************", dic);
        NSDictionary *infoDic = [dic objectForKey:@"info"];
        
        NSArray *dataArray = [infoDic objectForKey:@"data"];
        if (!([dataArray count] == 0)) { ///有歌词的话
            
            NSDictionary *lyricdic = [dataArray objectAtIndex:0];
            NSString *linshi = [lyricdic objectForKey:@"id"];
            NSString *lyaddress = [NSString stringWithFormat:@"http://api.raventech.cn/api/music/lyric?id=%@",linshi];
            NSString *superaddressly = [lyaddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
#warning 图片的加载
            NSString *picstr = [lyricdic objectForKey:@"pic_url"];
            NSString *pppp = [picstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            singer.pic_url = [NSString stringWithFormat:@"%@", picstr];
#warning 设置图片的关于
            [self.myImageview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", pppp]] placeholderImage:nil options:SDWebImageRetryFailed];
#warning 设磨砂效果
            self.myImageview.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.height);
            
            self.myImageview.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2);
            [self.picutureScroll removeFromSuperview];
            
            self.picutureScroll  = [[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)] autorelease];
            //            self.picutureScroll.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2);
            self.picutureScroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, 0);
            CGFloat aaa = [UIScreen mainScreen].bounds.size.height / 3;
            [self.picutureScroll addSubview:self.myImageview];
            self.picutureScroll.delegate = self;
            //            [self.picutureScroll setBackgroundColor:[UIColor greenColor]];
            //这儿
            //            self.picutureScroll.contentOffset = CGPointMake(self.playScroll.contentOffset.x / [UIScreen mainScreen].bounds.size.width * aaa, 0);
            UIImage *image = [[UIImage alloc]init];
            image = self.myImageview.image;
            NSLog(@"%f, %f", self.myImageview.image.size.width, self.myImageview.image.size.height);
            //            UIFont *font = [UIFont systemFontSize];
            [self.view addSubview:self.picutureScroll];
            [self.view sendSubviewToBack:self.picutureScroll];
            
            //            [self.view addSubview:self.myImageview];
#warning 歌词的网络请求
            AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
            manger.responseSerializer = [AFHTTPResponseSerializer serializer];
            [manger GET:superaddressly parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                NSData *data2 = [NSData dataWithData:responseObject];
                NSDictionary *lyricinfodic = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
                NSDictionary *ininin = [lyricinfodic objectForKey:@"info"];
                
                NSString *lyricStrsss = [ininin objectForKey:@"lyric"];
                //            NSLog(@"***********************\n%@\n*****************************", lyricStrsss);
                
                //            self.lyricArray = [NSArray array];
                //            self.lyricArray = [lyricStrsss componentsSeparatedByString:@"\n"];
                NSArray *arrays = [lyricStrsss componentsSeparatedByString:@"\n"];
                
                //            NSLog(@"%@", self.lyricArray);
                
                [self cutLyricArray:arrays];
                
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                NSLog(@"%@", error);
            }];
            
        }
        else
        {
            [self.lyricListTableView removeFromSuperview];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
    
}
- (void)cutLyricArray:(NSArray *)array
{
    self.lyricArray = [NSMutableArray array];
    for (NSString *str in array) {
        NSArray *array = [str componentsSeparatedByString:@"]"];
        NSMutableString *timestr = [array objectAtIndex:0];
        NSArray *array2 = [timestr componentsSeparatedByString:@"["];
        //        [timestr deleteCharactersInRange:NSMakeRange(0, 1)];
        if ([timestr hasPrefix:@"["]) {  //如果有时间的话
            //            NSLog(@"%@", array2); //取得了时间  02:23:23
            NSString *timestr2 = [array2 objectAtIndex:1];//*
            NSArray *array3 = [timestr2 componentsSeparatedByString:@":"];//*
            NSInteger min = [[array3 objectAtIndex:0] integerValue]; //分钟  02
            NSString  *timeStr3 = [array3 objectAtIndex:1];
            NSArray *array4 = [timeStr3 componentsSeparatedByString:@"."];
            NSInteger second = [[array4 objectAtIndex:0] integerValue];
            //            NSInteger msec = [[array4 objectAtIndex:1] integerValue];
            //            NSLog(@"%ld, %ld, %ld", min, second, msec);
            CGFloat inte = min * 60 + second;
            self.tag = inte;
            NSString *timetime = [NSString stringWithFormat:@"%f", inte];
            //            NSLog(@"inte == %f", inte);
            NSMutableDictionary *timedic = [NSMutableDictionary dictionary];
            [timedic setObject:timetime forKey:@"time"];
            [timedic setObject:[array objectAtIndex:1] forKey:@"lyric"];
            [self.lyricArray addObject:timedic];   //存满了时间
        } else
        {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:@"__" forKey:@"lyric"];
            [dic setObject:@"__" forKey:@"time"];
            [self.lyricArray addObject:dic];
        }
        
    }
    //    NSLog(@"%@", self.lyricArray);
    SingerState *single = [SingerState shareOfPlayState];
    for (int i = 0; i < 10; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSString *str = [NSString stringWithFormat:@"%ld", self.tag];
        [dic setObject:@"__" forKey:@"lyric"];
        [dic setObject:str forKey:@"time"];
        [self.lyricArray addObject:dic];
    }
    
    single.lyricArray = [NSMutableArray arrayWithArray:self.lyricArray];
    single.numOfTime = 0;
    
#warning 打算是请求到歌词后再铺歌词界面
    [self.lyricListTableView removeFromSuperview];
    [self.mengban removeFromSuperview];
    self.mengban = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size
                                                            .width, [UIScreen mainScreen].bounds.size.height)] autorelease];
    [self.mengban setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
    [self.playScroll addSubview:self.mengban];
    
    self.lyricListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 130, [UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.width - 40) style:UITableViewStylePlain];
    [self.lyricListTableView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.5]];
    //    [self.lyricListTableView setBackgroundColor:[UIColor colorWithRed:0.9 green:0.2 blue:0.2 alpha:1]];
    
    [self.lyricListTableView setBackgroundColor:[UIColor clearColor]];
    //    [self.view addSubview:self.lyricListTableView];
    self.lyricListTableView.delegate = self;
    self.lyricListTableView.dataSource = self;
    [self.lyricListTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellcell"];
    self.lyricListTableView.bounces = NO;
    self.lyricListTableView.separatorColor = [UIColor colorWithWhite:0.2 alpha:0.2];
    self.lyricListTableView.separatorColor = [UIColor clearColor];
    UIView *vv = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)] autorelease];
    [self.lyricListTableView addSubview:vv];
    [self.playScroll addSubview:self.lyricListTableView];
    [vv setBackgroundColor:[UIColor clearColor]];
    [self.playScroll addSubview:vv];
    //    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.width / 2 + 50,[UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.width / 2 / 5 * 2)];
    //    [view setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.2]];
    //    [self.view addSubview:view];
    //
    //    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.width / 2 + 50 + [UIScreen mainScreen].bounds.size.width / 2 / 5 * 3, [UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.width / 2 / 5 * 2)];
    //    [view2 setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.2]];
    //    [self.view addSubview:view2];
    //
    
    
    
    //    [self.view bringSubviewToFront:self.playScroll];

    
    self.playScroll.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
    
}
@end
